package com.ruoyi.system.modbus;

import com.ruoyi.common.utils.DateUtils;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;

public class ModbusTest {

    public static void main(String[] args) throws InterruptedException {
//        //开启ModbusTcpMaster连接
//        ModbusMaster masterA;
//        ModbusMaster masterB;
//        ModbusTcpMaster modbusTcpMasterA = new ModbusTcpMaster();
//        masterA = modbusTcpMasterA.getSlave("192.168.0.117", 500);
//        masterA.setTimeout(500);
//        ModbusTcpMaster modbusTcpMaster = new ModbusTcpMaster();
//        masterB = modbusTcpMaster.getSlave("192.168.0.118", 500);
//        masterB.setTimeout(500);
//
////        boolean c = Modbus4jWriteUtils.writeCoils(masterB, 1, 0,
////                new boolean[]{true, true, true, true, true, true, true, true, true, true});
////        boolean c1 = Modbus4jWriteUtils.writeCoils(masterA, 1, 0,
////                new boolean[]{true, true, true, true});
////        boolean b = Modbus4jWriteUtils.writeCoils(masterB, 1, 0,
////                new boolean[]{false, false, false, false, false, false, false, false, false, false});
////        boolean b1 = Modbus4jWriteUtils.writeCoils(masterA, 1, 0,
////                new boolean[]{false, false, false, false});
//
//
//        //2.1	控制单个继电器   站号、写入地址-输出点（0）、开关    （功能码05）
////        //打开站号为1输出点为1的继电器
//        System.out.println("执行modbus定时任务333-------------->" + DateUtils.getTime2());
//        boolean t01 = Modbus4jWriteUtils.writeCoil(masterB, 1, 0, true);
//        Thread.sleep(10);
////        //关闭站号为1输出点为1的继电器
//        boolean t02 = Modbus4jWriteUtils.writeCoil(masterB, 1, 0, false);
//        Thread.sleep(10);
//        boolean t011 = Modbus4jWriteUtils.writeCoil(masterB, 1, 0, true);
////        //关闭站号为1输出点为1的继电器
//        Thread.sleep(10);
//        boolean t021 = Modbus4jWriteUtils.writeCoil(masterB, 1, 0, false);
//        System.out.println("执行modbus定时任务3334-------------->" + System.currentTimeMillis());
////        //打开站号为1输出点为1的继电器
////        boolean t011 = Modbus4jWriteUtils.writeCoil(master,1, 1, true);
////        //关闭站号为1输出点为1的继电器
////        boolean t021 = Modbus4jWriteUtils.writeCoil(master,1, 1, false);
////
////        //批量写入(0x)  控制多个继电器
////        //站号为1输出点为1 2 3的继电器 修改状态为   打开  关闭  打开  （功能码0F）
////        boolean t03 = Modbus4jWriteUtils.writeCoils(master, 1, 0, new boolean[]{true, false, true, true, true});
//////
//////        //获取继电器状态
////        boolean t04 = Modbus4jReadUtil.readCoilStatus(master,1, 0,"test");
////        boolean t044 = Modbus4jReadUtil.readCoilStatus(master,1, 1,"test");
////
////        //获取继电器状态
////        //模拟量接液位传感器，获取液位高度，液位的高度与模拟量的数值  控制通道数值offset获取指定数据
//        Thread.sleep(10);
//        Number t05 = Modbus4jReadUtil.readInputRegisters(masterA, 1, 0, DataType.TWO_BYTE_INT_UNSIGNED, "test");
//        System.out.println("执行modbus定时任务333-------------->" + System.currentTimeMillis());
//        System.out.println("t05:" + t05);
//        Thread.sleep(10);
//        Number t055 = Modbus4jReadUtil.readInputRegisters(masterA, 1, 1, DataType.TWO_BYTE_INT_UNSIGNED, "test");
//        System.out.println("t05:" + t05);
//        Thread.sleep(10);
//        Number t0555 = Modbus4jReadUtil.readInputRegisters(masterA, 1, 2, DataType.TWO_BYTE_INT_UNSIGNED, "test");
//        System.out.println("t05:" + t05);
//        Thread.sleep(10);
//        Number t05555 = Modbus4jReadUtil.readInputRegisters(masterA, 1, 3, DataType.TWO_BYTE_INT_UNSIGNED, "test");
//        System.out.println("t05:" + t05);
//        Thread.sleep(10);
//        Number t055555 = Modbus4jReadUtil.readInputRegisters(masterA, 1, 4, DataType.TWO_BYTE_INT_UNSIGNED, "test");
//        System.out.println("t05:" + t05);
//        Thread.sleep(10);
//        boolean t0555555 = Modbus4jReadUtil.readInputStatus(masterA, 1, 0, "test");
//        System.out.println("执行modbus定时任务333-------------->" + System.currentTimeMillis());
//        System.out.println("t05:" + t05);

    /*    Thread t1 = new Thread(() -> {
            ModbusController.controller(1,1);
        }, "thread--1");
        t1.start();
        Thread t2 = new Thread(() -> {
            ModbusController.controller(2,2);
        }, "thread--2");
        t2.start();
        Thread t3 = new Thread(() -> {
            ModbusController.controller(1,3);
        }, "thread--3");
        t3.start();
        Thread t4 = new Thread(() -> {
            ModbusController.controller(2,4);
        }, "thread--4");
        t4.start();*/

        System.out.println("1111111111111111");


    }

    private static void test() {
        ModbusController.readCoilStatus(null, 0, 0, null);
    }


}
